package com.vcg.vdam.testbeandefinitioHttpApiInvoke;

import com.vcg.vdam.jdkDync.InvocationHandlerImpl;
import org.springframework.beans.factory.FactoryBean;

import javax.security.auth.Subject;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * A factory bean which produce HttpApi interface's implement by using proxyFactory
 *
 * @author huangxuyang
 * date 2018/11/1
 */
public class HttpApiProxyFactoryBean<T> implements FactoryBean<T> {

    private Class<T> interfaceClass;

    public Class<T> getInterfaceClass() {
        return interfaceClass;
    }

    public void setInterfaceClass(Class<T> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }


    @Override
    public T getObject() throws Exception {

        InvocationHandlerImpl invocationHandler = new InvocationHandlerImpl();
        return (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, invocationHandler);
    }

    @Override
    public Class<?> getObjectType() {
        return interfaceClass;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

}
